#include "bhxjglcamera.h"

BHXJGLCamera::BHXJGLCamera()
{

}

BHXJGLCamera::~BHXJGLCamera()
{

}

///获取俯仰角
const float & BHXJGLCamera::GetPitch()
{
    return  this->pitch;
}
///设置俯仰角
void BHXJGLCamera::SetPitch(const float & value,bool compute)
{
    float p;
    if(this->pitchlimit)
    {
        p=value>90?90:value;
        p=value<-90?-90:value;
    }
    else
    {
        p=std::fmod(value,360.0f);
    }

    this->pitch=p;
    this->setCBase();
    if(compute)
        this->setView();
}
///俯仰角限制
void BHXJGLCamera::PitchLimit(bool limit)
{
    this->pitchlimit=limit;
}

///获取偏航角
const float & BHXJGLCamera::GetYaw()
{
    return  this->yaw;
}
///设置偏航角
void BHXJGLCamera::SetYaw(const float & value,bool compute)
{
    float y = std::fmod(value,360.f);
    this->yaw=y;
    this->setCBase();
    if(compute)
        this->setView();
}



///获取滚转角
const float & BHXJGLCamera::GetRoll()
{
    return  this->roll;
}
///设置滚转角
void BHXJGLCamera::SetRoll(const float & value,bool compute)
{
    this->roll=std::fmod(value,360.0f);
    this->setCBase();
    if(compute)
        this->setView();
}

///获取摄像机位置
const glm::vec3 & BHXJGLCamera::GetPosition()
{
    return this->position;
}
///设置摄像机位置
void BHXJGLCamera::SetPosition(const glm::vec3 &value,bool compute)
{
    this->position=value;
    if(compute)
        this->setView();
}

void BHXJGLCamera::ForWard_Move(const float & value,bool compute)
{
    glm::vec3 center=(glm::vec3(cbase[2][0], cbase[2][1], cbase[2][2]));
    this->position= this->position - value*center;
    if(compute)
        this->setView();
}
void BHXJGLCamera::BackWard_Move(const float & value,bool compute)
{
    glm::vec3 center=(glm::vec3(cbase[2][0], cbase[2][1], cbase[2][2]));
    this->position= this->position + value*center;
    if(compute)
        this->setView();
}
void BHXJGLCamera::LeftWard_Move(const float & value,bool compute)
{
    glm::vec3 right=(glm::vec3(cbase[0][0], cbase[0][1], cbase[0][2]));
    this->position= this->position - value*right;
    if(compute)
        this->setView();
}
void BHXJGLCamera::RightWard_Move(const float & value,bool compute)
{
    glm::vec3 right=(glm::vec3(cbase[0][0], cbase[0][1], cbase[0][2]));
    this->position= this->position + value*right;
    if(compute)
        this->setView();
}

const glm::mat4 &BHXJGLCamera::View(bool compute)
{
    if(compute)
        this->setView();
    return view;
}
const glm::mat4 &BHXJGLCamera::CBase(bool compute)
{
    if(compute)
        this->setCBase();
    return cbase;
}

void BHXJGLCamera::setView()
{
    //up 即是y轴单位向量
    glm::vec3 up=(glm::vec3(cbase[1][0], cbase[1][1], cbase[1][2]));
    //center 即是z轴单位向量
    glm::vec3 center=(glm::vec3(cbase[2][0], cbase[2][1], cbase[2][2]));

    view= glm::lookAt(this->position, this->position - center, up);
}

void BHXJGLCamera::setCBase()
{
    //摄像机坐标空间的单位正交基底
    cbase= glm::mat4(1.0f);

    //偏航 围绕-y轴
    cbase=glm::rotate(cbase, glm::radians(this->yaw), glm::vec3(0, -1, 0));

    //俯仰 围绕x轴
    //cbase=glm::rotate(cbase, glm::radians(this->pitch), glm::vec3(cbase[0][0], cbase[0][1], cbase[0][2]));
    cbase=glm::rotate(cbase, glm::radians(this->pitch), glm::vec3(1, 0, 0));

    //滚转 围绕-z轴
    //cbase=glm::rotate(cbase, glm::radians(this->roll), glm::vec3(-cbase[2][0], -cbase[2][1], -cbase[2][2]));
    cbase=glm::rotate(cbase, glm::radians(this->roll), glm::vec3(0, 0, -1));
}
